/*
 * Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greendroid;

import java.util.List;

import org.gaeproxy.R;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

/**
 * A {@link QuickActionGrid} is an implementation of a {@link QuickActionWidget}
 * that displays {@link QuickAction}s in a grid manner. This is usually used to
 * create a shortcut to jump between different type of information on screen.
 * 
 * @author Benjamin Fellous
 * @author Cyril Mottier
 */
public class QuickActionGrid extends QuickActionWidget {

	private GridView mGridView;

	private OnItemClickListener mInternalItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> adapterView, View view,
				int position, long id) {
			getOnQuickActionClickListener().onQuickActionClicked(
					QuickActionGrid.this, position);
			if (getDismissOnClick()) {
				dismiss();
			}
		}
	};

	public QuickActionGrid(Context context) {
		super(context);

		setContentView(R.layout.gd_quick_action_grid);

		final View v = getContentView();
		mGridView = (GridView) v.findViewById(R.id.gdi_grid);
	}

	@Override
	protected void onMeasureAndLayout(Rect anchorRect, View contentView) {

		contentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(),
				MeasureSpec.EXACTLY), LayoutParams.WRAP_CONTENT);

		int rootHeight = contentView.getMeasuredHeight();

		int offsetY = getArrowOffsetY();
		int dyTop = anchorRect.top;
		int dyBottom = getScreenHeight() - anchorRect.bottom;

		boolean onTop = (dyTop > dyBottom);
		int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY
				: anchorRect.bottom - offsetY;

		setWidgetSpecs(popupY, onTop);
	}

	@Override
	protected void populateQuickActions(final List<QuickAction> quickActions) {

		mGridView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return quickActions.size();
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View view, ViewGroup parent) {

				TextView textView = (TextView) view;

				if (view == null) {
					final LayoutInflater inflater = LayoutInflater
							.from(getContext());
					textView = (TextView) inflater.inflate(
							R.layout.gd_quick_action_grid_item, mGridView,
							false);
				}

				QuickAction quickAction = quickActions.get(position);
				textView.setText(quickAction.mTitle);
				textView.setCompoundDrawablesWithIntrinsicBounds(null,
						quickAction.mDrawable, null, null);

				return textView;

			}
		});

		mGridView.setOnItemClickListener(mInternalItemClickListener);
	}

}
